package com.zt.teacher.service.impl;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.zt.common.common.ApiException;
import com.zt.common.common.CodeMsg;
import com.zt.common.pojo.Course;
import com.zt.common.utils.ClassUtil;
import com.zt.common.utils.RedisUtil;
import com.zt.teacher.dao.CourseDao;
import com.zt.teacher.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * description:
 * author:zt
 * date:2021-12-16
 */

@Service
@Transactional
public class CourseServiceImpl implements CourseService {

    private static final Log log = LogFactory.get();

    @Autowired
    private CourseDao courseDao;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * @param course 新增的课程
     * @return 返回该课程id
     */
    @Override
    public int insert(Course course) {
        log.info("新增课程：" + course);
        if (courseDao.getCourseByName(course.getName())!=null) {
            throw new ApiException(CodeMsg.COURSE_EXISTED);
        }
        courseDao.insert(course);
        return course.getId();
    }

    @Override
    public void delCourse(int courseId) {
        log.info("删除课程id：" + courseId);
        courseDao.delete(courseId);
    }

    @Override
    public void updateCourse(Course course) {
        log.info("更新课程：" + course);
        courseDao.update(course);
    }

    /**
     * redis存储格式： key=“2021-2022-2-hasStartClass” item=“2021-2022-2-startClass”,item存在过期时间
     * item存在即正在开课；key在item不在即开课结束；key不在即未开课
     * @param semester 学期
     * @param expired 天数
     */
    @Override
    public void setStartClass(String semester,int expired){
        //获取未来两个学期的开课情况 同一时间只能有一个学期开课
        for (int i = 0; i <= 1; i++) {
            String s = ClassUtil.getSemester(i);
            if (redisUtil.hasKey(s + "-startClass")) {
                log.error(s + "学期正在开课，不能开课");
                throw new ApiException(s + "学期正在开课");
            }
            if (redisUtil.hasKey(s + "-hasStartClass")){
                log.error(s + "学期已开课，不能开课");
                throw new ApiException(s + "学期已开课");
            }
        }

        Date time = new Date(System.currentTimeMillis() + (long) expired * 24 * 60 * 60 * 1000);
        String expireTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);
        log.info(semester + "学期开启开课成功，开课过期时间：" + expireTime);

        redisUtil.set(semester + "-hasStartClass",expireTime);
        redisUtil.set(semester + "-startClass",1, (long) expired * 24 * 60 * 60);
    }

    @Override
    public void setSelectClass(String semester,int expired){
        if (!redisUtil.hasKey(semester + "-hasStartClass"))
            throw new ApiException("还未开课，不能选课");
        if (redisUtil.hasKey(semester + "-hasSelectClass"))
            throw new ApiException("该学期已选课");

        Date time = new Date(System.currentTimeMillis() + (long) expired * 24 * 60 * 60 * 1000);
        String expireTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);
        log.info(semester + "学期开启选课成功，选课过期时间：" + expireTime);

        redisUtil.set(semester + "-hasSelectClass",expireTime);
        redisUtil.set(semester + "-selectClass",1,(long) expired * 24 * 60 * 60);
    }

    @Override
    public List<Course> getAllCourse() {
        log.info("获取所有课程");
        return courseDao.getAllCourse();
    }

    /**
     * 根据课程id判断是否为必修课
     * @param courseId 课程id
     * @return
     */
    @Override
    public boolean isCompulsory(int courseId) {
        Course course = courseDao.getCourseById(courseId);
        return course.getType()==0;
    }

}
